package com.avic.rong.questionOrAnswer.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.questionOrAnswer.activity.QuestionActivity;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.avic.rong.questionOrAnswer.bean.AssociateBean;
import com.avic.rong.search.activity.SearchHomePageActivity;

import java.util.List;

/**
 * Created by XinEr on 2019/8/8.
 * Describe:
 */
public class QuestionListAdapter extends RecyclerView.Adapter<QuestionListAdapter.ViewHolder> {
    private Context context;
    private List<AssociateBean> listDataLX;


    public QuestionListAdapter(Context context, List<AssociateBean> listDataLX) {
        this.context = context;
        this.listDataLX = listDataLX;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.question_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        AssociateBean associateBean = listDataLX.get(i);


        SpannableString spannableString = new SpannableString(associateBean.getTitle());
        Log.e("cjx", "onBindViewHolder: "+ SearchHomePageActivity.nowSearchStr);
        //ForegroundColorSpan
        int startIndex = associateBean.getTitle().toLowerCase().indexOf(QuestionActivity.searchTerm.toLowerCase());
        if (startIndex>=0){
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
            spannableString.setSpan(colorSpan, startIndex, startIndex+QuestionActivity.searchTerm.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

            viewHolder.tv_question.setText(spannableString);
        }else {
            viewHolder.tv_question.setText(associateBean.getTitle());
        }
        viewHolder.tv_answer_sum.setText("回答"+associateBean.getAnsNum());
        viewHolder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                context.startActivity(QuestionDetailsActivity.queryIntent(context, associateBean.getId()+""));
            }
        });
    }

    @Override
    public int getItemCount() {
        return listDataLX.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_question;
        private TextView tv_answer_sum;
        private LinearLayout ll_root;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_question=itemView.findViewById(R.id.tv_question);
            tv_answer_sum=itemView.findViewById(R.id.tv_answer_sum);
            ll_root=itemView.findViewById(R.id.ll_root);
        }
    }
}
